/* PhotoImport
 * Copyright by faena.com
 * 
 * 
*/
using System;
using Gtk;
using System.Xml;
using System.Text;

namespace PhotoImport
{
	public class SettingFile
	{
		private const string SettingFileName = "PhotoImportSettings.xml";

		public SettingFile ()
		{
			//Reload();
		}

		public void Save ()
		{
			string fileName = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			
			fileName = System.IO.Path.Combine (fileName, SettingFileName);
			
			using (XmlTextWriter xw = new XmlTextWriter (fileName, Encoding.Unicode)) {
				xw.Formatting = Formatting.Indented;
				xw.WriteStartDocument (true);
				xw.WriteStartElement ("PhotoImport");
				
				xw.WriteElementString ("MediaTypes", _mediaTypes);
				xw.WriteElementString ("DeleteAfterDownload", Bool2Str (_deleteAfterDownload));
				
							
				xw.WriteElementString ("TargetDir", _targetDir);
				
				xw.WriteElementString ("SourceDir", _sourceDir);

				xw.WriteElementString ("CreateSubfolder", Bool2Str (_createSubDir));
				
				int i = (int)_subfolderTemplate;
				xw.WriteElementString ("SubfolderTemplate", i.ToString ());
				
				xw.WriteElementString ("NewName", _newName);
				
				i = (int)_useTime;
				xw.WriteElementString ("FileTimeStamp", i.ToString ());
				
				xw.WriteEndElement ();
				
				xw.Close ();
				
			}
		}

		//Specify search pattern for media files. File names are case-sensitive. Recognized delimiters are ,; and space.
		private string _mediaTypes = "*.jpg, *.JPG, *.jpeg, *.png, *.avi, *.wmv, *.mp4, *.mpeg"; 
		public string MediaTypes {
			get { return _mediaTypes; }
			set { _mediaTypes = value; }
		}

		private bool _deleteAfterDownload = true;
		public bool DeleteAfterDownload {
			get { return _deleteAfterDownload; }
			set { _deleteAfterDownload = value; }
		}

			
		//Stores an html encoded string for safely store into xml
		private string _sourceDir = Environment.GetFolderPath (Environment.SpecialFolder.Desktop); 
		

		public string SourceDir 
		{
			get{
				
				return _sourceDir;
				//return System.Web.HttpUtility.UrlDecode(_sourceDir); 
			}
			set{
				_sourceDir = value;
				//_sourceDir = System.Web.HttpUtility.UrlPathEncode(value);
			}
		}
		
		//Stores an html encoded string for safely store into xml
		private string _targetDir = Environment.GetFolderPath (Environment.SpecialFolder.MyPictures); 

		public string TargetDir {


			get { 
				
				return _targetDir;
				//return System.Web.HttpUtility.UrlDecode(_targetDir); 
			}


			set { 
				_targetDir = value;
				//_targetDir = System.Web.HttpUtility.UrlPathEncode(value); 
			}
		}

		private bool _createSubDir = true;
		public bool CreateSubDir {
			get { return _createSubDir; }
			set { _createSubDir = value; }
		}

		public enum SubfolderTemplates
		{
				YearMonthDate,
				NewName,
				Year_YearMonthDate,
				Year_NewName,
				NewName_YearMonthDate,
				Year_Month_YearMonthDate,
				Year_Month,
				YearMonthDate_NewName,
		}
		private SubfolderTemplates _subfolderTemplate;
		public SubfolderTemplates SubfolderTemplate {

			get { return _subfolderTemplate; }

			set { _subfolderTemplate = value; }
		}

		private string _newName = "";
		public string NewName {
			get { return _newName; }
			set { _newName = value; }
		}

		public enum UseTimes
		{
			UseShotTime,
			UseCurrentTime
		}
		private UseTimes _useTime;
		public UseTimes UseTime {
			get { return _useTime; }
			set { _useTime = value; }
		}


		/// <summary>
		/// Convert a boolean to "Yes" or "No" 
		/// </summary>
		/// <param name="input">
		/// A <see cref="System.Boolean"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		private string Bool2Str (bool input)
		{
			if (input)
				return "Yes";
			else
				return "No";
		}

		/// <summary>
		/// Convert string into boolean. If input is empty or null, returns defaultValue. If input is "Yes", returns true. If input is "No", return false. Else returns defaultValue. 
		/// </summary>
		/// <param name="input">
		/// A <see cref="System.String"/>
		/// </param>
		/// <param name="defaultValue">
		/// A <see cref="System.Boolean"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/>
		/// </returns>
		private bool Str2Bool (string input, bool defaultValue)
		{
			if (string.IsNullOrEmpty (input))
				return defaultValue;
			if (string.Compare (input, "Yes", true) == 0)
				return true;
			if (string.Compare (input, "No", true) == 0)
				return false;
			
			return defaultValue;
		}

		/// <summary>
		/// Load settings from setting file. If file does not exists, returns false. Otherwise, returns true. 
		/// </summary>
		/// <returns>
		/// true = settings are loaded from setting file.
		/// false = setting file not found, all settings are using default values defined in SettingFile class.
		/// </returns>
		public bool Reload ()
		{
			string fileName = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
			
			fileName = System.IO.Path.Combine (fileName, SettingFileName);
			
			if (System.IO.File.Exists (fileName)) {
				XmlDocument xd = new XmlDocument ();
				xd.Load (fileName);
				XmlNode xn = xd.SelectSingleNode ("/PhotoImport/MediaTypes");
				if (xn != null)
					_mediaTypes = xn.InnerText;
				
				xn = xd.SelectSingleNode ("/PhotoImport/DeleteAfterDownload");
				if (xn != null)
					_deleteAfterDownload = Str2Bool (xn.InnerText, _deleteAfterDownload);
				
				
				xn = xd.SelectSingleNode ("/PhotoImport/SourceDir");
				if (xn != null)
					_sourceDir = xn.InnerText;		
				
				xn = xd.SelectSingleNode ("/PhotoImport/TargetDir");
				if (xn != null)
					_targetDir = xn.InnerText;
				
				xn = xd.SelectSingleNode ("/PhotoImport/CreateSubfolder");
				if (xn != null)
					_createSubDir = Str2Bool (xn.InnerText, _createSubDir);
				
				xn = xd.SelectSingleNode ("/PhotoImport/SubfolderTemplate");
				if (xn != null)
				{
					int i;
					if (int.TryParse (xn.InnerText, out i))
						_subfolderTemplate = (SubfolderTemplates)i;
				}
				xn = xd.SelectSingleNode ("/PhotoImport/NewName");
				if (xn != null)
					_newName = xn.InnerText;
				
				
				xn = xd.SelectSingleNode ("/PhotoImport/FileTimeStamp");
				if (xn != null) {
					int i;
					if (int.TryParse (xn.InnerText, out i))
						_useTime = (UseTimes)i;
				}
				return true;
			}
			else
				return false;
		}
	}
}

